/* 
 * File:   ThreadPool.h
 * Author: Lance
 *
 * Created on September 30, 2012, 12:32 AM
 */

#ifndef THREADPOOL_H
#define	THREADPOOL_H

#include "ClientHandler.h"
#include <semaphore.h>
#include <pthread.h>
#include <vector>
#include <queue>

using namespace std;

class ThreadPool {
public:
	ThreadPool();
	ThreadPool(int poolSize);
	ThreadPool(const ThreadPool& orig);
	virtual ~ThreadPool();
	void enqueue(ClientHandler * handler);
	ClientHandler * dequeue();
	void waitForWork();
	bool debug;
private:
	void initializeSemaphores();
	void initializeThreads();
	sem_t queueUnlocked; //queueSem, for protecting queue
	sem_t workToDo;	//threadSem, for telling threads when something is in the queue
	queue<ClientHandler *> clientQueue;	//a client queue, needs to be protected. Has a number of handlers. The thread will then call handler.handle
	int poolSize;
};


struct WorkerData
{
	ThreadPool * pool;
	sem_t * workToDo;
	WorkerData(ThreadPool * pool, sem_t * workToDo) {
		this->pool = pool;
		this->workToDo = workToDo;
	};
};

#endif	/* THREADPOOL_H */

